/*
 * File:   splitoptions.cxx
 * Author: RamneekHanda
 *
 * Created on Nov 22, 2012, 11:41:28 PM
 */

#include "splitoptions.hxx"
#include "../includes/request.hxx"

CPPUNIT_TEST_SUITE_REGISTRATION(splitoptions);

class MyRequest : public cis::Request {
public:
    std::vector<std::string> split(const std::string& params) {
        return cis::Request::splitOptions(params);
    }
    const Poco::Util::OptionSet& getOptions() {
        //NOOP;
    }
    std::string execute(const std::string&) {
        // NOOP
    }
} request;

splitoptions::splitoptions() {
}

splitoptions::~splitoptions() {
    
}
    
void splitoptions::testQuotedString() {
    std::string param = "--projectname llvm --makefile /workspace/llvm/Makefile --makeparams \"-C /Users/RamneekHanda/Documents/workspace/llvm/\"";
    std::vector<std::string> v = request.split(param);
    CPPUNIT_ASSERT(v.size() == 6);
    CPPUNIT_ASSERT(v.front() == "--projectname"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "llvm"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "--makefile"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "/workspace/llvm/Makefile"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "--makeparams"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "-C /Users/RamneekHanda/Documents/workspace/llvm/"); v.erase(v.begin());
}

void splitoptions::testLastEscape() {
    std::string param = "--projectname llvm --makefile /workspace/llvm/Makefile --makeparams \\";
    std::vector<std::string> v = request.split(param);
    CPPUNIT_ASSERT(v.size() == 6);
    CPPUNIT_ASSERT(v.front() == "--projectname"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "llvm"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "--makefile"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "/workspace/llvm/Makefile"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "--makeparams"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "\\"); v.erase(v.begin());
}

void splitoptions::testEscapeSpace() {
    std::string param = "--projectname\\ llvm --makefile /workspace/llvm/Makefile --makeparams \\";
    std::vector<std::string> v = request.split(param);
    CPPUNIT_ASSERT(v.size() == 5);
    CPPUNIT_ASSERT(v.front() == "--projectname\\ llvm"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "--makefile"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "/workspace/llvm/Makefile"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "--makeparams"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "\\"); v.erase(v.begin());
}

void splitoptions::testMissingQuote() {
    std::string param = "--projectname\\ llvm --makefile /workspace/llvm/Makefile --makeparams \"test";
    std::vector<std::string> v = request.split(param);
    CPPUNIT_ASSERT(v.size() == 5);
    CPPUNIT_ASSERT(v.front() == "--projectname\\ llvm"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "--makefile"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "/workspace/llvm/Makefile"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "--makeparams"); v.erase(v.begin());
    CPPUNIT_ASSERT(v.front() == "\"test"); v.erase(v.begin());
}